import numpy as np
import matplotlib.pyplot as plt

def logistic_map(r, x):
    return r * x * (1 - x)

def plot_bifurcation_diagram(r_values, x_values, color_values):
    plt.scatter(r_values, x_values, c=color_values, s=0.01, cmap='jet')
    plt.xlabel("r")
    plt.ylabel("x")
    plt.title("Logistic Bifurcation Diagram")
    plt.show()

# 设置参数
num_points = 1000  # 每个r值的迭代次数
num_bifurcation = 500  # bifurcation图的r值数量
r_min, r_max = 2.4, 4.0  # r的取值范围
x_min, x_max = 0, 1  # x的取值范围

# 生成r值的数组
r_values = np.linspace(r_min, r_max, num=num_bifurcation)

# 初始化数组用于存储数据点
x_values = np.random.uniform(x_min, x_max, num_points)
color_values = np.zeros(num_points)

# 迭代计算
for j in range(num_bifurcation):
    r = r_values[j]
    for i in range(num_points):
        x = x_values[i]
        x = logistic_map(r, x)
        if i > num_points // 2:
            color_values[i] = r

# 绘制分岔图
plot_bifurcation_diagram(r_values, x_values, color_values)
